home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.92 / crossfir / crossfire-0.92.5 / common / image.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  9KB  |  293 lines

  1. /*
  2.  * static char *rcsid_image_c =
  3.  *   "$Id: image.c,v 1.11 1996/01/02 10:26:47 master Exp $";
  4.  */
  5.  
  6. /*
  7.     CrossFire, A Multiplayer game for X-windows
  8.  
  9.     Copyright (C) 1994 Mark Wedel
  10.     Copyright (C) 1992 Frank Tore Johansen
  11.  
  12.     This program is free software; you can redistribute it and/or modify
  13.     it under the terms of the GNU General Public License as published by
  14.     the Free Software Foundation; either version 2 of the License, or
  15.     (at your option) any later version.
  16.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU General Public License
  23.     along with this program; if not, write to the Free Software
  24.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26.     The maintainer of this code can be reached at master@rahul.net
  27. */
  28.  
  29.  
  30.  
  31. #include <global.h>
  32. #include <stdio.h>
  33.  
  34. /* bmappair and xbm are used when looking for the image id numbe
  35.  * of a face by name.  xbm is sorted alphabetically so that bsearch
  36.  * can be used to quickly find the entry for a name.  the number is
  37.  * then an index into the new_faces array.
  38.  */
  39. New_Face *new_faces;
  40.  
  41. struct bmappair {
  42.     char *name;
  43.     unsigned int number;
  44. };
  45.  
  46. static struct bmappair *xbm=NULL;
  47.  
  48. /* only used in loader.c, to go from the numeric image id (which is
  49.  * used throughout the program) backto the standard name.
  50.  */
  51.  
  52. MapLook blank_look;
  53.  
  54. /* Following can just as easily be pointers, but 
  55.  * it is easier to keep them like this.
  56.  */
  57. New_Face *blank_face, *blocked_face, *stipple1_face, *stipple2_face;
  58. New_Face *potion_face;
  59.  
  60.  
  61. New_Face *inv_curse_face, *inv_damn_face, *inv_equip_face, *inv_lock_face,
  62.      *inv_magic_face, *inv_unpaid_face;
  63.  
  64. /* nroffiles is the actual number of bitmaps defined.
  65.  * nrofpixmaps is the higest numbers bitmap that is loaded.  With
  66.  * the automatic generation of the bmaps file, this is now equal
  67.  * to nroffiles.
  68.  *
  69.  * The xbm array (which contains name and number information, and
  70.  * is then sorted) contains nroffiles+1 entries.  the xbm_names
  71.  * array (which is used for converting the numeric face to
  72.  * a name) contains nrofpixmaps+1 entries.
  73.  */
  74. int nroffiles = 0, nrofpixmaps=0;
  75.  
  76. static int compar (struct bmappair *a, struct bmappair *b) {
  77.     return strcmp (a->name, b->name);
  78. }
  79.  
  80.  
  81. /*
  82.  * Returns the matching color in the coloralias if found,
  83.  * 0 otherwise.  Note that 0 will actually be black, so there is no
  84.  * way the calling function can tell if an error occurred or not
  85.  *
  86.  * CF 0.91.7: relocated from loader.c file - this perhaps can be
  87.  * declared static.
  88.  */
  89.  
  90. char find_color(char *name) {
  91.   int i;
  92.   for(i=0;i<NUM_COLORS;i++)
  93.     if(!strcmp(name,colorname[i][0]))
  94.       return i;
  95.   LOG(llevError,"Unknown color: %s\n",name);
  96.   return 0;
  97. }
  98.  
  99. /* This reads the lib/faces file, getting color and visibility information.
  100.  * it is called by ReadBmapNames.
  101.  */
  102.  
  103.  
  104. static void ReadFaceData()
  105. {
  106.     char buf[MAX_BUF], *cp;
  107.     New_Face *on_face=NULL;
  108.     FILE *fp;
  109.  
  110.     sprintf(buf,"%s/faces", LibDir);
  111.     if ((fp=fopen(buf,"r"))==NULL) {
  112.     perror("Can't open faces file");
  113.     printf("buf = %s\n", buf);
  114.     exit(-1);
  115.     }
  116.     LOG(llevDebug,"Reading faces...");
  117.     while (fgets(buf, MAX_BUF, fp)!=NULL) {
  118.     if (*buf=='#') continue;
  119.     if (!strncmp(buf,"end",3)) {
  120.         on_face = NULL;
  121.     }
  122.     else if (!strncmp(buf,"face",4)) {
  123.          int tmp;
  124.  
  125.          cp = buf + 5;
  126.          cp[strlen(cp)-1] = '\0';    /* remove newline */
  127.  
  128.          if ((tmp=FindFace(cp,-1))==-1) {
  129.         LOG(llevError,"Could not find face %s\n", cp);
  130.         continue;
  131.          }
  132.          on_face = &new_faces[tmp];
  133.     }
  134.     else if (on_face==NULL) {
  135.         LOG(llevError,"Got line with no face set: %s\n", buf);
  136.     }
  137.     else if (!strncmp(buf,"color_fg",8)) {
  138.         cp = buf + 9;
  139.         cp[strlen(cp)-1] = '\0';
  140.         on_face->fg = find_color(cp);
  141.     }
  142.     else if (!strncmp(buf,"color_bg",8)) {
  143.         cp = buf + 9;
  144.         cp[strlen(cp)-1] = '\0';
  145.         on_face->bg = find_color(cp);
  146.     }
  147.     else if (!strncmp(buf,"visibility",10)) {
  148.         on_face->visibility = atoi(buf + 11);
  149.     }
  150.     else LOG(llevDebug,"Got unknown line in faces file: %s\n", buf);
  151.     }
  152.     LOG(llevDebug,"done\n");
  153.     fclose(fp);
  154. }
  155.  
  156. /* This reads the bmaps.paths file to get all the bitmap names and
  157.  * stuff.  It only needs to be done once, because it is player
  158.  * independent (ie, what display the person is on will not make a
  159.  * difference.)
  160.  */
  161.  
  162. int ReadBmapNames () {
  163.     char buf[MAX_BUF], *p, *q;
  164.     FILE *fp;
  165.     int value, nrofbmaps = 0, i, fg,bg;
  166.  
  167.     sprintf (buf,"%s/bmaps", LibDir);
  168.     if ((fp=fopen(buf,"r"))==NULL) {
  169.     perror("Can't open bmaps file");
  170.     printf("buf = %s\n", buf);
  171.     exit(-1);
  172.     }
  173.     LOG(llevDebug,"Reading bmaps...");
  174.     
  175.     /* First count how many bitmaps we have, so we can allocate correctly */
  176.     while (fgets (buf, MAX_BUF, fp)!=NULL)
  177.     if(buf[0] != '#' && buf[0] != '\n' )
  178.         nrofbmaps++;
  179.     rewind(fp);
  180.     
  181.     xbm = (struct bmappair *) malloc(sizeof(struct bmappair) * (nrofbmaps + 1));
  182.     memset (xbm, 0, sizeof (struct bmappair) * (nrofbmaps + 1));
  183.     
  184.     while(fgets (buf, MAX_BUF, fp)!=NULL) {
  185.     if (*buf == '#')
  186.         continue;
  187.     
  188.     p = (*buf == '\\') ? (buf + 1): buf;
  189.     if (!(p = strtok (p , " \t")) || !(q = strtok (NULL , " \t\n"))) {
  190.         LOG(llevDebug,"Warning, syntax error: %s\n", buf);
  191.         continue;
  192.     }
  193.     value = atoi (p);
  194.     xbm[nroffiles].name = strdup_local(q);
  195.     xbm[nroffiles].number = value;
  196.     nroffiles++;
  197.     if(value > nrofpixmaps)
  198.         nrofpixmaps = value;
  199.     }
  200.     fclose(fp);
  201.  
  202.     LOG(llevDebug,"done (got %d/%d/%d)\n",nrofpixmaps,nrofbmaps,nroffiles);
  203.  
  204.     new_faces = (New_Face *)malloc(sizeof(New_Face) * (nrofpixmaps+1));
  205.     fg = find_color ("black");
  206.     bg = find_color ("khaki");
  207.     for (i = 0; i <= nrofpixmaps; i++) {
  208.     new_faces[i].name = "";
  209.     new_faces[i].number = i;
  210.     new_faces[i].fg = fg;
  211.     new_faces[i].bg = bg;
  212.     }
  213.     for (i = 0; i < nroffiles; i++) {
  214.     new_faces[xbm[i].number].name = xbm[i].name;
  215.     }
  216.  
  217.     nrofpixmaps++;
  218.  
  219.     qsort (xbm, nrofbmaps, sizeof(struct bmappair), (int (*)())compar);
  220.  
  221.     ReadFaceData();
  222.  
  223.     /* Actually forcefully setting the colors here probably should not
  224.      * be done - it could easily create confusion.
  225.      */
  226.     blank_face = &new_faces[FindFace(BLANK_FACE_NAME, 0)];
  227.     blank_look.face = blank_face;
  228.     blank_look.flags = 0;
  229.     blank_face->fg = find_color ("black");
  230.     blank_face->bg = find_color ("khaki");
  231.  
  232.     blocked_face = &new_faces[FindFace (BLOCKED_FACE_NAME,0)];
  233.     blocked_face->fg = find_color ("black");
  234.     blocked_face->bg = find_color ("black");
  235.     stipple1_face = &new_faces[FindFace (STIPPLE1_FACE_NAME,0)];
  236.     stipple1_face->fg = find_color ("white");
  237.     stipple1_face->bg = find_color ("black");
  238.     stipple2_face = &new_faces[FindFace (STIPPLE2_FACE_NAME,0)];
  239.     stipple2_face->fg = find_color ("white");
  240.     stipple2_face->bg = find_color ("black");
  241.     potion_face  = &new_faces[FindFace(POTION_FACE_NAME,0)];
  242.     potion_face->fg = find_color("light_blue");
  243.     potion_face->bg = find_color("khaki");
  244.     inv_curse_face = &new_faces[FindFace(INV_CURSE_FACE_NAME,0)];
  245.     inv_damn_face = &new_faces[FindFace(INV_DAMN_FACE_NAME,0)];
  246.     inv_equip_face = &new_faces[FindFace(INV_EQUIP_FACE_NAME,0)];
  247.     inv_lock_face = &new_faces[FindFace(INV_LOCK_FACE_NAME,0)];
  248.     inv_magic_face = &new_faces[FindFace(INV_MAGIC_FACE_NAME,0)];
  249.     inv_unpaid_face = &new_faces[FindFace(INV_UNPAID_FACE_NAME,0)];
  250.  
  251.     return nrofpixmaps;
  252. }
  253.  
  254.  
  255. /* This returns an the face number of face 'name'.  Number is constant
  256.  * during an invocation, but not necessarily between versions (this
  257.  * is because the faces are arranged in alphabetical order, so
  258.  * if a face is removed or added, all faces after that will now
  259.  * have a different number.
  260.  *
  261.  * the parameter error determines behaviour.  If a face is
  262.  * not found, then error is returned.  This can be useful if
  263.  * you want some default face used, or can be set to negative
  264.  * so that it will be known that the face could not be found
  265.  * (needed in client, so that it will know to request that image
  266.  * from the server)
  267.  */
  268. int FindFace (char *name, int error) {
  269.     int i;
  270.     struct bmappair *bp, tmp;
  271.     char *p;
  272.  
  273.  
  274.     /* Using actual numbers for faces is a very bad idea.  This is because
  275.      * each time the archetype file is rebuilt, all the face numbers
  276.      * change.
  277.      */
  278.     if ((i = atoi(name))) {
  279.     LOG(llevError,"Warning: Integer face name used: %s\n", name);
  280.     return i;
  281.     }
  282.  
  283.     if ((p = strchr (name, '\n')))
  284.     *p = '\0';
  285.  
  286.     tmp.name = name;
  287.     bp = (struct bmappair *)bsearch 
  288.     (&tmp, xbm, nroffiles, sizeof(struct bmappair), (int (*)())compar);
  289.  
  290.     return bp ? bp->number : error;
  291. }
  292.  
  293.